services:

  zookeeper:
    image: confluentinc/cp-zookeeper:6.2.4
    healthcheck:
      test: [ "CMD", "nc", "-vz", "localhost", "2181" ]
      interval: 10s
      timeout: 3s
      retries: 3
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    ports:
      - 22181:2181

  kafka:
    image: confluentinc/cp-kafka:6.2.4
    depends_on:
      zookeeper:
        condition: service_healthy
    ports:
      - 29092:29092
    healthcheck:
      test: [ "CMD", "nc", "-vz", "localhost", "9092" ]
      interval: 10s
      timeout: 3s
      retries: 3
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_LISTENERS: OUTSIDE://:29092,INTERNAL://:9092
      KAFKA_ADVERTISED_LISTENERS: OUTSIDE://localhost:29092,INTERNAL://kafka:9092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,OUTSIDE:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1

  kafka-ui:
    image: provectuslabs/kafka-ui:latest
    container_name: kafka-ui
    ports:
      - "8080:8080"
    restart: always
    depends_on:
      kafka:
        condition: service_healthy
    environment:
      KAFKA_CLUSTERS_0_NAME: local
      KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:9092

#  service-db:
#    image: postgres:14.7-alpine
#    environment:
#      POSTGRES_USER: username
#      POSTGRES_PASSWORD: password
#    healthcheck:
#      test: ["CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}"]
#      interval: 10s
#      timeout: 3s
#      retries: 3
#    ports:
#      - "15432:5432"
#    volumes:
#      - ./infrastructure/db/create_db.sql:/docker-entrypoint-initdb.d/create_db.sql
#    restart: unless-stopped

#  pgadmin:
#    container_name: pgadmin4_container
#    image: dpage/pgadmin4:7
#    restart: always
#    environment:
#      PGADMIN_DEFAULT_EMAIL: admin@admin.com
#      PGADMIN_DEFAULT_PASSWORD: root
#    ports:
#      - "5050:80"

  kafka-topics-generator:
    image: confluentinc/cp-kafka:6.2.4
    depends_on:
      kafka:
        condition: service_healthy
    entrypoint: [ '/bin/sh', '-c' ]
    command: |
      "
      # blocks until kafka is reachable
      kafka-topics --bootstrap-server kafka:9092 --list
      
      echo -e 'Creating kafka topics'
      kafka-topics --bootstrap-server kafka:9092 --create --if-not-exists --topic send-order-event --replication-factor 1 --partitions 2
      
      echo -e 'Successfully created the following topics:'
      kafka-topics --bootstrap-server kafka:9092 --list
      "
